package com.shuoyue.ycgk.ui.mok;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Mok;
import com.shuoyue.ycgk.entity.MokType;
import com.shuoyue.ycgk.entity.Rule;
import com.shuoyue.ycgk.ui.common.RuleContract;
import com.shuoyue.ycgk.ui.course.info.CourseInfoActivity;
import com.shuoyue.ycgk.ui.mine.mok.MyMokActivity;
import com.shuoyue.ycgk.utils.GlideUtil;
import com.shuoyue.ycgk.utils.MyRichTextHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MokListActivity extends BaseMvpActivity<MokContract.Presenter>
        implements MokContract.View, RuleContract.View {
    @BindView(R.id.tv_right)
    TextView tvRight;

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.introduct)
    TextView introduct;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    RuleContract.Presenter rulePresenter;
    MokAdapter adapter;
    //    MokType mokType;
    int subjectId;

//    public static void start(Context source, MokType type) {
//        source.startActivity(new Intent(source, MokListActivity.class)
//                .putExtra("type", type)
//        );
//    }

    public static void start(Context source, int subjectId) {
        source.startActivity(new Intent(source, MokListActivity.class)
                .putExtra("subjectId", subjectId)
        );
    }

    @Override
    protected int getLayoutId() {
        return (R.layout.activity_mok_list);
    }

    @Override
    public void initData() {
//        mokType = (MokType) getIntent().getSerializableExtra("type");
        subjectId = getIntent().getIntExtra("subjectId", -1);
        mPresenter = new MokContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getMokList(subjectId);
        rulePresenter = new RuleContract.Presenter();
        rulePresenter.attachView(this);
        rulePresenter.getRule(2);
//        GlideUtil.loadImage(mContext, mokType.getCover(), img);
    }

    @Override
    protected void initView() {
        pageTitle.setText("模考列表");
        tvRight.setText("我的模考");
        tvRight.setTextColor(ContextCompat.getColor(mContext,R.color.themeColor));
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        recyclerView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8)));
        adapter = new MokAdapter(null);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener((adapter, view, position) -> {
            Mok mok = (Mok) adapter.getItem(position);
            switch (view.getId()) {
                case R.id.lay_course:
                    startActivity(new Intent(mContext, CourseInfoActivity.class)
                            .putExtra("id", mok.getRecommendProduct().getId()));
                    break;
                case R.id.report:
                    MokDetailActivity.start(mContext, mok.getMockId());
                    break;
            }
        });
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getMokList(subjectId);
            refreshLayout.finishRefresh(2000);
        });
    }

    @Override
    public void setMokList(List<Mok> mokList) {
        adapter.resetData(mokList);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setMokTypeList(List<MokType> mokList) {

    }


    @Override
    public void setRule(Rule userInfo) {
        GlideUtil.loadImage(mContext, userInfo.getImage(), img);
        MyRichTextHelper.showRich(mContext, userInfo.getContent(), introduct);
    }


    @OnClick({R.id.back, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tv_right:
                startActivity(new Intent(mContext, MyMokActivity.class));

                break;
        }
    }
}
